package UMC.Security;

import UMC.Data.Utility;

import java.util.UUID;

public abstract class Identity {

    static class Identification extends Identity {

        Identification(UUID sn, String alias, String name, String... roles) {
            this._id = sn;
            this._Alias = alias;
            this._name = name;
            this._roles = roles;
            this._organizes = new String[0];
        }

        Identification(UUID sn, String alias, String name, String[] roles, String... organizes) {
            this._id = sn;
            this._Alias = alias;
            this._name = name;
            this._roles = roles;
            this._organizes = organizes;
        }

        UUID _id;
        String _Alias;

        @Override
        public String alias() {

            return _Alias;

        }

        @Override
        public boolean isInRole(String role) {
            if (_roles != null) {
                for (int r = 0; r < _roles.length; r++) {
                    String s = _roles[r];
                    if (s.equals(AccessToken.AdminRole)) {
                        return true;
                    }
                    if (s.equalsIgnoreCase(role)) {
                        return true;
                    }
                }
            }
            return false;
        }

        String[] _roles;
        String _name;
        String[] _organizes;

        @Override
        public UUID id() {
            return _id;
        }

        @Override
        public String name() {
            return this._name;
        }

        @Override
        public String[] roles() {
            return _roles;
        }

        @Override
        public String[] organizes() {
            return _organizes;
        }

        @Override
        public boolean IsOrganizeMember(String organizeName) {
            if (_organizes == null || Utility.isEmpty(organizeName)) {
                return false;
            } else {
                for (int r = 0; r < _organizes.length; r++) {
                    String s = _organizes[r];

                    if (s.equalsIgnoreCase(organizeName)) {
                        return true;
                    }
                }
            }
            return false;
        }
    }


    public boolean isAuthenticated() {

        if (!Utility.isEmpty(this.name()) && !this.name().equals("?")) {
            return true;
        } else {

            return false;
        }

    }

    public abstract String[] roles();

    public abstract String[] organizes();

    public abstract UUID id();

    public abstract String name();

    public abstract String alias();

    public abstract boolean isInRole(String role);

    public abstract boolean IsOrganizeMember(String organizeName);


    public static Identity create(String name) {
        return new Identification(null, null, name);
    }

    public static Identity create(String name, String alias) {
        return new Identification(null, alias, name);
    }

    public static Identity create(UUID sn, String name, String alias, String... roles) {
        return new Identification(sn, alias, name, roles);
    }

    public static Identity create(UUID sn, String name, String alias, String[] roles, String... organizes) {
        return new Identification(sn, alias, name, roles, organizes);
    }
}
